﻿
namespace log_mgr {
	using System;
	using System.Diagnostics;
	using log4net;
	using i_core;

	public class log4net_log : i_log {
		public log_type type => log_type.log4net;

		public string user { get; set; } = "system";

		/// <summary>
		/// default info (0x1e)
		/// debug(0xff), info(0x1e), warn(0x1c), error(0x18), fatal(0x10), nolog(0x00)
		/// </summary>
		public log_level log_level { get; set; } = log_level.info;

		public bool IsFatalEnabled => (((int)log_level >> 4) & 0x01) == 1;

		public bool IsErrorEnabled => (((int)log_level >> 3) & 0x01) == 1;

		public bool IsWarnEnabled => (((int)log_level >> 2) & 0x01) == 1;

		public bool IsInfoEnabled => (((int)log_level >> 1) & 0x01) == 1;

		public bool IsDebugEnabled => ((int)log_level & 0x01) == 1;



		public log4net_log() {
#if DEBUG
			log_level = log_level.debug;
#endif
		}

		public log4net_log(string user, bool islog) {
#if DEBUG
			log_level = log_level.debug;
#endif
			this.user = user;
		}

		public message get_msg(string m, string cl) {
			return new message() { user = user, cl = cl, msg = m };
		}

		public message get_msg(Exception ex, string cl) {
			return new message() { user = user, cl = cl, msg = ex.Message };
		}


		#region method debug 

		public void Debug(object message) {

			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Debug(get_msg(message.ToString(), methor_name));

		}

		public void Debug(object message, params object[] propertyValues) {

			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Debug(get_msg(string.Format(message.ToString(), propertyValues), methor_name));


		}

		public void Debug(object message, Exception exception) {
			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Debug(get_msg(message.ToString(), methor_name), exception);

		}

		public void DebugFormat(string format, params object[] args) {
			throw new NotImplementedException();
		}

		#endregion


		#region method Info 
		public void Info(object message, Exception exception) {
			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Info(get_msg(message.ToString(), methor_name), exception);

		}

		public void Info(object message) {

			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Info(get_msg(message.ToString(), methor_name));

		}

		public void Info(object message, params object[] propertyValues) {
			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Info(get_msg(string.Format(message.ToString(), propertyValues), methor_name));

		}

		public void InfoFormat(string format, params object[] args) {
			throw new NotImplementedException();
		}

		#endregion


		#region method Warn 
		public void Warn(object message) {

			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Warn(get_msg(message.ToString(), methor_name));

		}

		public void Warn(object message, Exception exception) {
			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Warn(get_msg(message.ToString(), methor_name), exception);
		}

		public void Warn(object message, params object[] propertyValues) {
			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Warn(get_msg(string.Format(message.ToString(), propertyValues), methor_name));

		}


		#endregion


		#region method error 
		public void Error(object message) {
			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Error(get_msg(message.ToString(), methor_name));
		}

		public void Error(object message, params object[] propertyValues) {
			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Error(get_msg(string.Format(message.ToString(), propertyValues), methor_name));

		}

		public void Error(object message, Exception exception) {
			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Error(get_msg(message.ToString(), methor_name), exception);

		}

		public void ErrorFormat(string format, params object[] args) {
			throw new NotImplementedException();
		}

		#endregion


		#region method Fatal 
		public void Fatal(object message) {

			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Fatal(get_msg(message.ToString(), methor_name));
		}

		public void Fatal(object message, Exception exception) {
			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Fatal(get_msg(message.ToString(), methor_name), exception);
		}

		public void Fatal(object message, params object[] propertyValues) {
			var trace = new StackTrace();
			var frame = trace.GetFrame(1);
			var method = frame.GetMethod();
			var j_p = method.DeclaringType.FullName.IndexOf('+');
			var methor_name = method.DeclaringType.FullName.Substring(0, j_p <= 0 ? method.DeclaringType.FullName.Length : j_p);

			var Log = LogManager.GetLogger(method.DeclaringType);

			Log.Fatal(get_msg(string.Format(message.ToString(), propertyValues), methor_name));
		}

		public void FatalFormat(string format, params object[] args) {
			throw new NotImplementedException();
		}


		#endregion

	}

}
